package kjj.android.video.app;

import java.io.IOException;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.ShutterCallback;
//import android.graphics.Camera;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class AndroidVideoActivity extends Activity implements OnClickListener, 
SurfaceHolder.Callback{
	
	private Button btnOpen;
	private Button btnClose;
	private Button btnCap;
	private SurfaceView sv;
	private SurfaceHolder sh;
	private Camera mCamera;
	boolean isView = false;
	private ShutterCallback mShutterCallback = new ShutterCallback() {
		
		@Override
		public void onShutter() {
			// TODO Auto-generated method stub
			
		}
	};
	
	private PictureCallback mRawCallback = new PictureCallback() {
		
		@Override
		public void onPictureTaken(byte[] data, Camera camera) {
			// TODO Auto-generated method stub
			
		}
	};
	private PictureCallback mjpegCallback = new PictureCallback() {
		
		@Override
		public void onPictureTaken(byte[] data, Camera camera) {
			// TODO Auto-generated method stub
			Bitmap bm = BitmapFactory.decodeByteArray(data, 0, data.length);
			ImageView imgv = (ImageView)findViewById(R.id.imageView1);
			imgv.setImageBitmap(bm);
//			isView = false;
//			mCamera.release();
//			mCamera = null;
//			initCamera();
			camera.startPreview();
		}
	};
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //set up full-screen display
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(
        		WindowManager.LayoutParams.FLAG_FULLSCREEN, 
        		WindowManager.LayoutParams.FLAG_FULLSCREEN);
        
        
        
        setContentView(R.layout.main);
        
        btnCap = (Button)findViewById(R.id.buttonCapture);
        btnClose = (Button)findViewById(R.id.buttonClose);
        btnOpen = (Button)findViewById(R.id.buttonOpen);
        btnCap.setOnClickListener(this);
        btnClose.setOnClickListener(this);
        btnOpen.setOnClickListener(this);
        
        sv = (SurfaceView)findViewById(R.id.surfaceView1);
        sh = sv.getHolder();
        sh.addCallback(this);
        sh.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        
    }

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v == btnOpen){
			initCamera();
		}
		else if (v == btnClose){
			if (mCamera != null && isView) {
				isView = false;
				mCamera.stopPreview();
				mCamera.release(); //release the camera resource.
				mCamera = null;
			}
		}
		else if (v == btnCap){
			if (isView) {
				mCamera.takePicture(mShutterCallback, mRawCallback, mjpegCallback);
			}
			else {
				String t = getResources().getString(R.string.toast_openCamera);
				Toast.makeText(this, t, Toast.LENGTH_LONG).show();
				
			}
	
			
		}
	}

	private void initCamera() {
		// TODO Auto-generated method stub
		if (!isView) {
			mCamera = Camera.open();
		}
		if (mCamera != null && !isView) {
			try {
				Camera.Parameters cpParameters = mCamera.getParameters();
				cpParameters.setPictureFormat(PixelFormat.JPEG);
//				cpParameters.setPreviewSize(320, 240);
//				cpParameters.setPreviewFrameRate(3);
//				cpParameters.setJpegQuality(85);
//				cpParameters.setPictureSize(320, 240);
				
				mCamera.setParameters(cpParameters);
				mCamera.setDisplayOrientation(90);
				mCamera.setPreviewDisplay(sh);
				mCamera.startPreview();
			} catch (IOException e) {
				// TODO: handle exception
				e.printStackTrace();
			}
			
			isView = true;
		}
	}
	
}